package com.action.api;

import com.bean.*;
import com.blade.mvc.annotation.GetRoute;
import com.blade.mvc.annotation.Path;
import com.blade.mvc.http.Request;
import com.blade.mvc.http.Response;
import com.google.gson.Gson;
import com.util.FileUtil;
import com.util.StatisticsUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.*;

@Slf4j
@Path
public class ApiAction {

    private DataStatistics dataStatistics = new DataStatistics();

    @GetRoute("/statistics")
    public void statistics(Response response, Request request) {

        response.header("Access-Control-Allow-Origin", "*");
        response.header("Access-Control-Allow-Credentials", "true");
        response.header("Access-Control-Allow-Methods", "PUT, POST, GET, OPTIONS, DELETE");
        response.header("Access-Control-Max-Age", "3600");
        response.header("Access-Control-Allow-Headers", "content-type, authorization");
        try {
            String module = request.query("module").get();
            String anInterface = request.query("interface").get();
            String name = request.query("name").get();
            String type = request.query("type").get();
//        log.info(module + anInterface + name);
            if (module != null && anInterface != null && name != null && type != null && type.trim().length() > 0 && module.trim().length() > 0 && anInterface.trim().length() > 0 && name.trim().length() > 0) {
                dataStatistics.setModuleName(module);
                dataStatistics.setInterfaceName(anInterface);
                dataStatistics.setSaveName(name);
                dataStatistics.setTimeStr(System.currentTimeMillis());
                if ("1".equals(type)) {
                    StatisticsUtil.add(dataStatistics, true);
                } else {
                    StatisticsUtil.add(dataStatistics, false);
                }
                response.json("{\"code\":200}");
            } else {
                response.json("{\"code\":-1}");
            }
        } catch (Exception e) {
            response.json("{\"code\":-1,\"msg\":\"" + e.getMessage() + "\"}");
        }

    }

    @GetRoute("/getOneFileString")
    public void getOneFileString(Response response, Request request) {
        String s = FileUtil.getRunPath() + File.separator + "db";
        response.json(getFileString(s));
    }

    private String getFileString(String s) {
        try {
            File file = new File(s);
            String[] list = file.list();
            return new Gson().toJson(list);
        } catch (Exception e) {
            return "{\"code\":-1}";
        }
    }


    @GetRoute("/getTwoFileString")
    public void getTwoFileString(Response response, Request request) {
        String s = FileUtil.getRunPath() + File.separator + "db" + File.separator + request.query("path").get();
        response.json(getFileString(s));
    }


    @GetRoute("/getData")
    public void getData(Response response, Request request) {
        String callback = request.query("callback").get();
        String path = request.query("path").get();
        String fileName = request.query("fileName").get();
        List<ShowData> showData = FileUtil.readFile(path + File.separator + fileName);
        String json = new Gson().toJson(showData);
        String s = callback + "(" + json + ")";
        response.json(s);
    }


}
